package com.hg.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.dao.UserDao;
import com.hg.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * created by skh on 2019/12/15
 */
@Service
@Slf4j
//@DS("slave")
public class UserServiceImpl extends ServiceImpl<UserDao, User>  {

    @Autowired
    private UserDao userDao;




    //@DS("master")
    public void addUserToMaster(String userName) {
        userDao.addUser(userName);
        log.info("新增用户成功");
    }

    //@DS("slave")
    public void addUserToSlave(String userName) {
        userDao.addUser(userName);
        log.info("新增用户成功");
    }


    //@DS("master")
    public void listFromMaster() {
        List<User> users = userDao.selectUserList();
        log.info("从主库查询用户列表:{}", users);

        log.info("从从库查询用户列表:{}", users);
    }

    public void listFromSlave() {
        List<User> users = userDao.selectUserList();
        log.info("从从库查询用户列表:{}", users);
    }


    public void addUser() {
        User user = new User();
        user.setName("skh");
        userDao.insert(user);
        log.info("新增用户成功");
        throw new RuntimeException("自定义异常");
    }
}
